var UBBEditor = function(elem) {
	this.textbox = null;
	this.toolbar = null;
	this.formaters = null;
	this.realtext = null;

	extend(elem, this).init();
	return elem;
}

UBBEditor.prototype = {
	
	init: function() {
		this.hotKeys = {Send: 'Enter'};
	
		this.realtext = getElementsByClass('TextBox', this)[0];
		hide(this.realtext);
		
		var textboxElement = document.createElement("DIV");
		this.realtext.parentNode.insertBefore(textboxElement, this.realtext);
		new Panel(textboxElement);
		
		
		this.textbox = document.createElement("DIV");
		textboxElement.appendChild(this.textbox);

		this.textbox.unselectable = 'off';
		this.textbox.className = 'Editor';
		this.textbox.style.overflow = 'auto';
		
        if (!this.textbox.contentEditable) {
        	this.textbox.designMode = "on";
        } else {
        	this.textbox.contentEditable = true;
        }
        if (this.textbox.spellcheck !== undefined) {  // Firefox
        	this.textbox.spellcheck = false;
        }
        this.textbox.parent = this;

		this.toolbar = getElementsByClass('ToolBar', this)[0];
		this.formaters = getElementsByClass('Formater', this);

		for(var i=0;i<this.formaters.length;i++) {
			this.formaters[i].parent = this;
			this.formaters[i].unselectable = 'off';
			bind(this.formaters[i], 'click', this.formatAction);
		}
		
		var buttons = getElementsByClass('Button', this);
		for(var i=0;i<buttons.length;i++) {
			new Button(buttons[i]);
		}

		this.textbox.onkeydown = this.keyPress;
		
		this.tbPanel = new VSplitPanel(getElementsByClass('TextBoxPanel', this)[0], 20);
		var buttons = getElementsByClass('SendButtonPanel', this)[0];
		
		this.tbPanel.append(textboxElement, 0);
		this.tbPanel.append(buttons, 90);
	},
	
	resizeTo: function(size) {
		var msize = setElementSize(this, size);
		setElementSize(this.toolbar, new Size(msize.width, 30));
		this.tbPanel.resizeTo(new Size(msize.width, msize.height-30));
	},
	
	clear: function() {
		this.textbox.innerHTML = "";
	},
	
	keyPress: function(e) {
	     var key;
	     var e = window.event || e;
	     var elem = e.srcElement || e.target;
		
	     if(window.event) key = e.keyCode;     // IE
	     else key = e.which;// firefox
	     
	     if(elem.parent.hotKeys.Send == 'Ctrl+Enter') {
	    	 if(e.ctrlKey && key == 13) {
	    		 if(typeof elem.parent.onHotKey == 'function')
	    			 elem.parent.onHotKey(elem, 'Send');
	    		 return false;
	    	 }
	     } else if(!e.ctrlKey && elem.parent.hotKeys.Send == 'Enter') {
	    	 if(key == 13) {
	    		 if(typeof elem.parent.onHotKey == 'function')
	    			 elem.parent.onHotKey(elem, 'Send');
	    		 return false;
	    	 }
	     }
	     return true;
	},
	
	setHotKey: function(type, data) {
		this.hotKeys[type] = data;
	},
	
	formatAction: function(elem, e) {
    	if(!elem.parent) return;
    	elem.parent.insert(elem.getAttribute('data'), null);
    },
    
    insert: function(act, data) {
    	this.textbox.focus();
    	var text = null;
		switch(act) {
		case 'Bold':
		case 'Italic':
		case 'Underline':
			document.execCommand(act, false, data);
			break;
		case 'Face':
			text = '<img src="'+data.src+'" data="'+data.data+'"/>';
		case 'Text':
			if(text == null) text = data;
			if(document.all){
				document.selection.createRange().pasteHTML(text) ;
			}else{ 
				document.execCommand('InsertHtml',false, text);
			}
			break;
		}
	},
	
	/*
	 * textChange: function(elem, e) { elem.parent.realtext.value =
	 * trim(elem.parent.textbox.innerHTML); },
	 */
	
	parse: function(elem) {
		var htmlText = '';
		var nodes = elem.childNodes;
		if(nodes.length < 0) return '';
		
		for(var i=0;i<nodes.length;i++) {
				var j = nodes[i];
				if(j.nodeName == "#text") {
					htmlText += j.data;
				} else if(j.nodeName == 'IMG') {
					var src = j.getAttribute('src');
					var data = j.getAttribute('data');
					if(data) {
						htmlText += '[FACE]'+data+'[/FACE]';
					} else {
						htmlText += '[IMG]'+src+'[/IMG]';
					}
				} else if(j.nodeName == 'SPAN') {
					var start='', end = '';
					if(j.style.textDecoration == 'underline') {
						start += '[U]';
						end += '[/U]';
					}
					if(j.style.fontStyle == 'italic') {
						start += '[I]';
						end += '[/I]';
					}
					if(j.style.fontWeight == 'bold') {
						start += '[B]';
						end += '[/B]';
					}
					htmlText += start + this.parse(j) + end;
				} else if(j.nodeName == 'STRONG' || j.nodeName == 'B') {
					htmlText += '[B]'+ this.parse(j) +'[/B]';
				} else if(j.nodeName == 'EM') {
					htmlText += '[I]'+ this.parse(j) +'[/I]';
				} else if(j.nodeName == 'U') {
					htmlText += '[U]'+ this.parse(j) +'[/U]';
				} else {
					if(j.nodeName == 'BR' || j.nodeName == 'DIV' || j.nodeName == 'P') {
						htmlText += "[BR]";
					}
					htmlText += this.parse(j);
				}
			}
		return htmlText;
	},
	
	text: function() {
		if(arguments.length == 0) {
			var text = this.parse(this.textbox);
			if(text == null) return '';
			
			return text = html2text(trim(text));
		} else {
			this.clear();
			this.insert('Text', arguments[0]);
		}
	}
}